/*
 * @Date: 2019-12-2
 * @Author: xuechengwu <xuechengwu@erayt.com>
 * @Description: 十字层
 */
import BaseLayer from './BaseLayer';
import Line from '../base/Line';
import Point from '../core/Point';
import DashLine from '../base/DashLine';
import Color from '../core/Color';
import Text from '../base/Text';
import Reactangle from '../base/Rectangle';

/**
 * 十字层处理默认处理函数
 * @param options
 * @returns {{vLabel: string, hLabel: *}}
 */
const processData = options => {
  return {
    hLabel: options.price,
    vLabel: ''
  };
};

export default class CurveLayer extends BaseLayer {
  constructor(canvas, options) {
    super(canvas);
    this.width = options.width;
    this.height = options.height;
    this.xStep = options.xStep || 0;
    this.yStep = options.yStep || 0;
    this.min = options.min || 0;
    this.max = options.max || 0;
    this.color = options.color || '#333333';
    this.fontColor = options.fontColor || '#FFFFFF';
    this.backgroundColor = options.backgroundColor || '#333333';
    this.size = options.size || 20;
    this.processData = options.processData || processData;
  }

  /**
   * 十字出现回调
   * @param point
   * @param callback
   */
  onRun(point, callback) {
    this.childs.splice(0, this.childs.length);
    const x = Math.round((point.x - this.position.x) / this.xStep) * this.xStep + this.xStep / 2;
    const vLine = new Line(this.canvas, {
      from: new Point(this.position.x + x, this.height + this.position.y),
      to: new Point(this.position.x + x, this.position.y)
    });
    const hLine = new DashLine(this.canvas, {
      from: new Point(this.position.x, point.y),
      to: new Point(this.position.x + this.width, point.y),
      dashLine: [3, 8]
    });
    vLine.setColor(new Color(this.color));
    hLine.setColor(new Color(this.color));
    const price = ((point.y - this.position.y) / this.yStep + this.min);
    const shift = Math.round((point.x - this.position.x) / this.xStep);
    const { hLabel , vLabel } = this.processData({ price, shift: Math.round((point.x - this.position.x) / this.xStep)});
    const hText = new Text(this.canvas, {
      text: hLabel,
      size: this.size,
      font: 'PingFang SC',
      color: this.fontColor
    });
    hText.setPosition(this.position.x + 2, point.y);
    const hBackRect = new Reactangle(this.canvas, {
      width: hLabel.length * this.size * 0.625 + 4,
      height: this.size + 4,
      color: this.backgroundColor
    });
    hBackRect.alpha = 0.6;
    hBackRect.setPosition(this.position.x, point.y - 2);
    const vText = new Text(this.canvas, {
      text: vLabel,
      size: this.size,
      font: 'PingFang SC',
      color: this.fontColor
    });
    vText.setPosition(point.x - this.size * 0.625 / 2, this.height - this.size - 2);
    const vBackRect = new Reactangle(this.canvas, {
      width: vLabel.length * this.size * 0.625 + 4,
      height: this.size + 4,
      color: this.backgroundColor
    });
    vBackRect.alpha = 0.6;
    vBackRect.setPosition(point.x - this.size * 0.625 / 2 - 2, this.height - this.size - 4);
    this.addChild(vLine, hLine, vBackRect, hBackRect, vText, hText);
    callback && callback(shift, price);
  }

  /**
   * 十字停止回调
   * @param point
   */
  onStop(point) {
    this.childs.splice(0, this.childs.length);
  }

  /**
   * 点击检测
   * @param point
   * @returns {boolean}
   */
  containsPoint(point) {
    return point.x >= 0
      && point.x <= this.width
      && point.y >= this.position.y
      && point.y <= this.position.y + this.height;
  }
}
